package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class PurchaseItem implements Serializable, Comparable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Double unitPrice;
    private Double shippingRate;
    private Double subTotal;
    private int quantity;
    private Double discount;
    @OneToOne
    private Product product;
    @Temporal(value = TemporalType.DATE)
    private Date requestDate;
    @OneToOne
    private Address shippingAddress;
    private String status;
    private Double outstandingAmount;
    private Boolean isDelivered;
    @OneToMany
    private List<BatchRequired> batchesRequired = new ArrayList<BatchRequired>();

    public PurchaseItem() {
    }

    public PurchaseItem(Double unitPrice, Double shippingRate, Double subTotal, Integer quantity, Double discount, Product product, Date requestDate, Address shippingAddress, String status, Double outstandingAmount) {
        this.unitPrice = unitPrice;
        this.shippingRate = shippingRate;
        this.quantity = quantity;
        this.discount = discount;
        this.product = product;
        this.requestDate = requestDate;
        this.shippingAddress = shippingAddress;
        this.subTotal = subTotal;
        this.status = status;
        this.outstandingAmount = outstandingAmount;
        this.isDelivered = false;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the unitPrice
     */
    public Double getUnitPrice() {
        return unitPrice;
    }

    /**
     * @param unitPrice the unitPrice to set
     */
    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    /**
     * @return the subTotal
     */
    public Double getSubTotal() {
        return subTotal;
    }

    /**
     * @param subTotal the subTotal to set
     */
    public void setSubTotal(Double subTotal) {
        this.subTotal = subTotal;
    }

    /**
     * @return the quantity
     */
    public int getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /**
     * @return the discount
     */
    public double getDiscount() {
        return discount;
    }

    /**
     * @param discount the discount to set
     */
    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    
    
     @Override
    public int compareTo(Object obj) {
        PurchaseItem poi = (PurchaseItem) obj;
        if (poi.product.getCode().compareTo(this.product.getCode()) < 0) {
            return 1;
        } else if (poi.product.getCode().compareTo(this.product.getCode()) > 0) {
            return -1;
        } else {
            if (poi.quantity < this.quantity) {
                return 1;
            } else if (poi.quantity > this.quantity) {
                return -1;
            } else {
                if (poi.unitPrice < this.unitPrice) {
                    return 1;
                } else if (poi.unitPrice > this.unitPrice) {
                    return -1;
                } else {
                    if (poi.discount < this.discount) {
                        return 1;
                    } else if (poi.discount > this.discount) {
                        return -1;
                    }
                    else
                    {
                        if (poi.shippingRate < this.shippingRate)
                            return 1;
                        else if (poi.shippingRate > this.shippingRate)
                            return -1;
                    }
                }

            }

        }

        return 0;
    }

    
    

    /**
     * @return the shippingRate
     */
    public Double getShippingRate() {
        return shippingRate;
    }

    /**
     * @param shippingRate the shippingRate to set
     */
    public void setShippingRate(Double shippingRate) {
        this.shippingRate = shippingRate;
    }

    /**
     * @return the requestDate
     */
    public Date getRequestDate() {
        return requestDate;
    }

    /**
     * @param requestDate the requestDate to set
     */
    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    /**
     * @return the shippingAddress
     */
    public Address getShippingAddress() {
        return shippingAddress;
    }

    /**
     * @param shippingAddress the shippingAddress to set
     */
    public void setShippingAddress(Address shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the outstandingAmount
     */
    public Double getOutstandingAmount() {
        return outstandingAmount;
    }

    /**
     * @param outstandingAmount the outstandingAmount to set
     */
    public void setOutstandingAmount(Double outstandingAmount) {
        this.outstandingAmount = outstandingAmount;
    }

    /**
     * @return the isDelivered
     */
    public Boolean getIsDelivered() {
        return isDelivered;
    }

    /**
     * @param isDelivered the isDelivered to set
     */
    public void setIsDelivered(Boolean isDelivered) {
        this.isDelivered = isDelivered;
    }

    /**
     * @return the batchesRequired
     */
    public List<BatchRequired> getBatchesRequired() {
        return batchesRequired;
    }

    /**
     * @param batchesRequired the batchesRequired to set
     */
    public void setBatchesRequired(List<BatchRequired> batchesRequired) {
        this.batchesRequired = batchesRequired;
    }
}
